﻿var defaultImg = '/EMSP_CMS/images/imgprew.png';
$(document).ready(function(){
	getSource();
	initEvent();
});
/**
 * 加载数据
 */
var myPagination;
function initEvent(){
	  $("#submitbutton").click(function(){
		   var formData = $("#queryForm").serialize(); //序列化表单
		   formData = decodeURIComponent(formData, true); //解码
		   formData += "&fresourcetype=视频&fdeleted=0";
		   $.fn.debug("开始指定加载");
		   myPagination.onLoad({param:formData});
		   $.fn.debug("结束指定加载");
	});
	$("#order").click(function(){
		if($('#checked').val()){
			$('#checked').val('');
			$("span[class=orderTime]").css("backgroundPosition","100% -2100px");
		}else{
			$('#checked').val('1');
			$("span[class=orderTime]").css("backgroundPosition","100% -2320px");
		}
		$("#submitbutton").click();
	});
}
function getSource(){
	var formData = "&fresourcetype=视频&fdeleted=0";
	myPagination = $("div[class=holder]").myPagination({
		currPage: 1,
		pageNumber: 15,
		cssStyle:'grayr',
		limit : 10,
		panel:{
			tipInfo_on:true,
			tipInfo_css:{
				width:"22px",height:'12px'
			}
		},
		ajax: {
		  on: true,
		  url: "resourceList",
		  dataType: 'json',
		  param:formData,
		  ajaxStart:function(){
			  ZENG.msgbox.show(" 正在加载中，请稍后...", 6, 10000);
		  },onClick:function(page){
			  $.fn.debug(page);
		  },
		  callback:function(data){
			  ZENG.msgbox.hide(); //隐藏加载提示
			  $.fn.debug(data.source);
			  bindGridList(data);
		  }
		}
	});
}
function bindGridList(data){
	$("#total").html('总共找到'+data.total+'条数据');
	var html= '';
	var iw = 150, ih = 110;
	if(data.total > 0){
		$.each(data.source,function(i,resource){
			var fname = resource.fname;
			//图片缩放
			var zoomRate = Math.max( Math.max( resource.fwidth / iw, 1 ), Math.max( resource.fheight / ih, 1 ));
			var w = resource.fwidth / zoomRate;
			w = w == 0 ? 150:w;
			var h = resource.fheight / zoomRate;
			h = h == 0 ? 110 : h;
			html += '<div class="div" id="web'+i+'" title="'+fname+'" style="float: left; padding:8px; width:195px; height:170px;" onclick="ischecked(this)" ondblclick="editAjax('+resource.id+')"><img src="'+((resource.fbillurl == "") ? defaultImg : resource.fbillurl)+'" width="'+w+'" height="'+h+'"/><br />';
			html += '<span><input id="source'+ resource.id +'" value="'+ resource.fresourceurl +'" type="hidden"></span>';
			if(resource.fcount == 0){
				html += '<input type="checkbox" name="web" onchange="checkedInput(this)" onclick="ischecked(document.getElementById(\'web'+i+'\'))" id="td'+ i +'" alt="'+resource+'" value="'+resource.id+'"/>';
			}
			html += (fname.length > 10 ? fname.substring(0,10)+"...":fname)+'<br />';
			html += '使用率：'+resource.fcount+'次<br />';
			html += '上映时间：'+resource.freleasetime+'</span></div>';
		});
		html += '<div style="clear: both;"></div>';
	}else{
		html += "<br/><div style='width:99%;height:40px;'>暂无数据</div>";
	}
	$("#resource").html(html);
}
/**
 * 选中子元素
 * @param obj
 */
function check(obj){
	if(obj.checked){
		var checkbox = $("[name='web']");
		checkbox.attr("checked",'true');//全选 
		$("[class='div']").css("background-color","#D8F79D");
	}else{
		$("[name='web']").removeAttr("checked");//取消全选 
		$("[class='div']").css("background-color","");
	}
}
/**
 * 当前选中项
 * @param obj
 */
function checkedInput(obj){
	var id = obj.id.replace("td","");
	if(obj.checked)
		$("#web"+id).css("background-color","#D8F79D");
	else
		$("#web"+id).css("background-color","");
}
/**
 * 单击行时，选中的则取消，未选中的则选中
 * @param obj
 */
function ischecked(obj){
	var id = obj.id;
	var trObj = $("#"+id);
	var tdId = "td"+id.replace("web","");
	var objCheck = $("#"+tdId);
	if(objCheck.attr("checked")){
		objCheck.removeAttr("checked");//取消选中 
		trObj.css("background-color","");
	}else{
		objCheck.attr("checked",'true');//选中
		trObj.css("background-color","#D8F79D");
	}
}
/**
 * 获取所有选中的id
 * @returns {Array}
 */
function checkedInfo(){
	var ids = new Array();
	$("[name='web'][checked]").each(function(){  
		ids.push($(this).val());  
	});
	return ids;  
}
/**
 * 加载数据
 * @param id
 */
function editAjax(id){
	var data = {"id":id};
	$.ajax({
		url:"queryById",
		data:data,
		type:"post",
		dataType:"json",
		success:function(data){
			bindGridEdit(data);
		},
		error:function(data){
			$.growlUI('数据加载出错'); 
		}
	});
}
/**
 * 编辑加载数据
 * @param id
 */
function editResource(){
	var ids = checkedInfo();
	if(ids.length == 1){
		editAjax(ids[0]); 
	}else if(ids.length == 0){
		var html = "<div style='vertical-align: middle;'><img src='/EMSP_CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要修改的信息</div>";	
		var	footerhtml='<input type="image" alt="Cancel" src="/EMSP_CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
		Box("editHtml",10,10,html,footerhtml,901);
	}else if(ids.length > 1){
		var html = "<div style='vertical-align: middle;'><img src='/EMSP_CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;只能修改1条信息！</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/EMSP_CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
		Box("editHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 绑定编辑数据
 * @param data
 */
function bindGridEdit(resource){
	var table = '<form action="updateResource" method="post" name="resourceForm" id="resourceForm">';
	table += '<div class="tab-page" id="baseinfo" style="display: block;">';
	table += '<table width="535" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">';
	table += '<tr><td height="25" width="80">资源名称</td><td><input name="id" type="hidden" id="id" value="'+resource.id+'"/>';
	table += '<input name="fname" type="text" id="fname" size="50" value="'+isNull(resource.fname)+'" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">资源类型</td><td><input class="text" name="fresourcetype" type="text" id="fresourcetype" size="50" value="'+isNull(resource.fresourcetype)+'" disabled="disabled" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">资源分类</td><td><input class="text" name="fresourcekind" type="text" id="fresourcekind" size="50" value="'+isNull(resource.fresourcekind)+'" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">导演</td><td><input class="text" name="fdirector" type="text" id="fdirector" value="'+isNull(resource.fdirector)+'" size="50" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">演员</td><td><input class="text" name="factor" type="text" id="factor" value="'+isNull(resource.factor)+'" size="50" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">上映时间</td><td><input class="text" name="freleasetime" type="text" id="freleasetime" onclick="WdatePicker({dateFmt:\'yyyy-MM-dd HH:mm:ss\'})" value="'+isNull(resource.freleasetime)+'" size="30" class="Wdate" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">影片海报</td><td style="vertical-align: middle;"><input class="text" name="fbillurl" type="hidden" id="fbillurl" value="'+isNull(resource.fbillurl)+'"/><img src="'+((resource.fbillurl == undefined) ? defaultImg : resource.fbillurl)+'" id="bill" width="100" height="65" ondblclick="getBill()"><input type="button" value="修改" onclick="getBill();"></td></tr>';
	table += '<tr><td height="25">码率</td><td><input name="frate" type="text" id="frate" size="50" value="'+isNull(resource.frate)+' " style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">大小</td><td><input name="fsize" type="text" id="fsize" size="50" value="'+isNull(resource.fsize)+' " style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">影片简介</td><td><textarea name="fsummary" type="text" id="fsummary" style="width:320px;height:50px;resize: none;"/>'+isNull(resource.fsummary)+'</textarea></td></tr>';
    table += '</table></div></form>';
	var	footerhtml='<input type="image" alt="Cancel" src="/EMSP_CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'resourceHtmlcloseBtn\').click();"/>&nbsp;&nbsp;&nbsp;<input type="image" alt="Submit" src="/EMSP_CMS/images/box/submit_button.jpg" class="btn" id="submit" onclick="submitResource()" />';
	Box("resourceHtml",600,410,table,footerhtml,910);
}

/**
 * 提交保存网站数据
 */
function submitResource(){
	if(formIsNull($("#resourceForm")))
		$("#resourceForm").ajaxSubmit({
			url:"updateResource",
			type:'post',
			dataType:'json',
			success:function(data){
				$.growlUI('数据修改成功');
				$("#cancel").click();
//				getSource();
				$('a[title=1]').click();
			},
			error:function(msg){
				$.growlUI('数据保存失败'); 
			}
		});
}

/**
 * 删除站点信息
 */
function checkedDeletedResource(flag,isDeleted){
	var ids = checkedInfo();
	if(ids.length > 0){
		var s = "";
	    for(var i=0;i<ids.length ;i++){
	        s +=ids[i]+",";
	    }
	    if(s != ""){
	    	s = s.substring(0,s.length-1) ;
	    }
		var data = {"ids":s,"flag":flag,"isDelete":isDeleted};
		$.ajax({
			url:"checkedDeleted",
			data:data,
			type:"post",
			success:function(data){
				$.growlUI('成功操作数据'+data+'条');
//				getSource();
				$('a[title=1]').click();
			},
			error:function(data){
				$.growlUI('数据操作失败'); 
			}
		});
		$("#AllCheck").attr("checked",false);
	}else{
		var html = "<div style='vertical-align: middle;'><img src='/EMSP_CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要操作的信息</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/EMSP_CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
		Box("deleteHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 删除数据
 */
function deletedResource(){
	var ids = checkedInfo();
	if(ids.length > 0){
		if(confirm('您确定删除吗？删除后无法恢复')){
			var s = "";
		    for(var i=0;i<ids.length ;i++){
		        s +=ids[i]+",";
		    }
		    if(s != ""){
		    	s = s.substring(0,s.length-1) ;
		    }
			var data = {"ids":s};
			$.ajax({
				url:"batchDelete",
				data:data,
				type:"post",
				success:function(data){
					$.growlUI('成功删除数据'+data+'条');
//					getSource(undefined,undefined,null,1,'recycResource');
					$('a[title=1]').click();
				},
				error:function(data){
					$.growlUI('数据删除失败'); 
				}
			});
		}
	}else{
		var html = "<div style='vertical-align: middle;'><img src='/EMSP_CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要删除的信息</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/EMSP_CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
		Box("deleteHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 判断是否为空
 * @param val
 * @returns
 */
function isNull(val){
	if(!val){
		val = "";
	}
	return val;
}
/**
 * 绑定数据
 * @param data
 */
function bindGridBill(data){
	var html= '';
	$.each(data.source,function(i,resource){
		var fname = resource.fname; 
		html += '<div class="div" id="web'+(i+20)+'" style="float: left; padding:8px; width:180px;" onclick="billSelected(\'td'+(i+20)+'\')"><img src="'+resource.fresourceurl+'" width="150" height="110" ondblclick="setBillValue(this.src);"/><br />';
		html += '<input type="radio" name="bill" onclick="billSelected(this.id)" id="td'+ (i+20) +'" value="'+resource.fresourceurl+'"/>';
		html += (fname.length > 10 ? fname.substring(0,10):fname)+'<br />';
		html += '使用率：'+resource.fcount+'次&nbsp;&nbsp;';
		html += '分辨率:'+resource.fwidth+'×'+resource.fheight+'</div>';
	});
	html += '<div style="clear: both;"></div>';
	$("#boxSource").html(html);
}
/**
 * 设置值
 */
function getBillValue(){
	var src = $('input[type=radio]:checked').val();
	setBillValue(src);
}
/**
 * 获取海报信息
 */
var order = "";
function getBill(c){
	if(c){
		if(this.order){
			this.order = "";
			$("span[class=orderTimes]").css("backgroundPosition","100% -2320px");
		}else{
			this.order = c;
			$("span[class=orderTimes]").css("backgroundPosition","100% -2100px");
		}
	}
	if(!c){
		var	footerhtml='<input type="image" alt="Cancel" src="/EMSP_CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'billImgcloseBtn\').click();"/>&nbsp;&nbsp;&nbsp;<input type="image" alt="Submit" src="/EMSP_CMS/images/box/determine_button.jpg" class="btn" id="submit" onclick="getBillValue()" />';
		var html = '<div class="billBox" style="width:99%; height:404px; border: 1px solid #B4C9C6; padding:1px;">';
		html += '<div class="optTools" id="optTools"style="width:99.5%; padding:1px;"></div><div class="contentBox" id="boxSource"style="width:99.5%; height:370px; padding:1px; border: 1px solid #B4C9C6;"></div><div class="holder">';
		html += '<div class="pager" id="pagerBox"></div><div class="clear"></div></div></div>';
		Box("billImg",880,400,html,footerhtml,999,920);
		var opt = '&nbsp;&nbsp;列出： 从 <input id="startTime" name="startTime" onClick="WdatePicker()" style="width: 100px;" type="text" class="Wdate">';
		opt += '至 <input id="endTime" name="endTime" onClick="WdatePicker()" style="width: 100px;" type="text" class="Wdate">';
		opt += '&nbsp;关键词: <input name="boxname" id="boxname" style="width: 110px;" type="text"><input class="inputButton" name="submitbutton" value="查询" onclick="getBill(1)" id="submitbutton" type="button">';
		opt += '<b>排序</b><a class="order" id="order" href="javascript:void(0);" onclick="getBill(1);"><span class="orderTimes">时间</span></a></span>';
		$("#optTools").html(opt);
	}
	//搜索功能
	var startdate = $("#startTime").val();
	var enddate = $("#endTime").val();
	var name = $("#boxname").val();
	$("#pagerBox").myPagination({
		currPage: 1,
		pageNumber: 15,
		cssStyle:'grayr',
		panel:{
			tipInfo_on:true,
			tipInfo_css:{
				width:"22px",height:'12px'
			}
		},
		ajax: {
		  on: true,
		  url: "resourceList",
		  dataType: 'json',
		  param:"fresourcetype=图片&fchecked="+this.checked+"&fdeleted=0&faddtime="+startdate+"&fupdatetime="+enddate+"&fname"+name+"&limit=8&fchecked="+this.order,
		  ajaxStart:function(){
			  ZENG.msgbox.show(" 正在加载中，请稍后...", 6, 10000);
		  },onClick:function(page){
			  $.fn.debug(page);
		  },
		  callback:function(data){
			  ZENG.msgbox.hide(); //隐藏加载提示
			  $.fn.debug(data.source);
			  bindGridBill(data);
		  }
		}
	});
}
/**
 * 选择模版，关闭模版层
 * @param id
 * @param val
 */
function setBillValue(val){
	
	$("#fbillurl").val(val);//设置模版值
	$("#bill").attr("src",val);
	$box("billImgcloseBtn").click();//关闭模版
}
/**
 * 选中bill
 */
function billSelected(id){
	$("#"+id).attr("checked",'true');//全选 
	var arr = $('input[type=radio]');
	$.each(arr,function(i,radio){
		var inputId = radio.id;
		var inputObj = $("#"+inputId);
		var divId = "web"+inputId.replace("td","");
		var obj = $("#"+divId);
		if(radio.checked){
			inputObj.attr("checked",'true');//全选 
			obj.css("background-color","#D8F79D");
		}else{
			inputObj.removeAttr("checked");//取消全选 
			obj.css("background-color","");
		}
	});
}